package com.aaa.sys.service.impl;

import com.aaa.common.bo.User;
import com.aaa.common.constants.BusinessConstant;
import com.aaa.sys.dao.UserDao;
import com.aaa.sys.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Sha512Hash;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 用户信息表(User)表服务实现类
 *
 * @author yuxiujie
 * @since 2021-01-20 09:34:31
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer userId) {
        return this.userDao.queryById(userId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<User> queryAllByLimit(int offset, int limit) {
        return this.userDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        user.setPassword("123456");
        user.setDelFlag("0");
        user.setCreateBy("admin");
        user.setCreateTime(new Date());
        String salt = UUID.randomUUID().toString().substring(0, 15);
        Sha512Hash sha512Hash = new Sha512Hash(user.getPassword(), salt, BusinessConstant.CredentialsPassword.HASH_ITERATIONS);
        //保存颜值
        user.setSalt(salt);
        //保存密码
        user.setPassword(sha512Hash.toString());
        this.userDao.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        if (!"".equals(user.getPassword()) && user.getPassword() != null) {
            String salt = UUID.randomUUID().toString().substring(0, 15);
            Sha512Hash sha512Hash = new Sha512Hash(user.getPassword(), salt, BusinessConstant.CredentialsPassword.HASH_ITERATIONS);
            //保存颜值
            user.setSalt(salt);
            //保存密码
            user.setPassword(sha512Hash.toString());
        }
        user.setUpdateBy("admin");
        user.setUpdateTime(new Date());
        this.userDao.update(user);
        return this.queryById(user.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.userDao.deleteById(userId) > 0;
    }

    /**
     * 分页查询多条数据
     *
     * @param user User
     * @return 对象列表
     */
    @Override
    public PageInfo<User> queryAll(int pageNo, int pageSize, User user) {
        PageHelper.startPage(pageNo, pageSize);
        return new PageInfo<>(this.userDao.queryAll(user));
    }


    @Override
    public User getUserByUserName(String userName) {
        User user = new User();
        user.setUserName(userName);
        user.setDelFlag("0");
        user.setStatus("0");
        List<User> users = this.userDao.queryAll(user);
        if (!CollectionUtils.isEmpty(users)) {
            return users.get(0);
        }
        return null;
    }

    @Override
    public List<User> checkRoleName(Integer userId, String userName) {
        return this.userDao.checkRoleName(userId, userName);
    }

    @Override
    public boolean resetPassword(Integer userId) {
        User user = new User();
        user.setUserId(userId);
        user.setPassword("123456");
        user.setUpdateBy("admin");
        user.setUpdateTime(new Date());
        String salt = UUID.randomUUID().toString().substring(0, 15);
        Sha512Hash sha512Hash = new Sha512Hash(user.getPassword(), salt, BusinessConstant.CredentialsPassword.HASH_ITERATIONS);
        //保存颜值
        user.setSalt(salt);
        //保存密码
        user.setPassword(sha512Hash.toString());
        return this.userDao.update(user) > 0;
    }

    /**
     * 功能描述:登陆成功之后修改登陆时间和登陆IP
     * @param userId 用户Id
     * @return void
     */
    @Override
    public void updateLoginTimeAndIp(Integer userId) {
        //获取RequestAttributes
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        //从获取RequestAttributes中获取HttpServletRequest的信息
        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
        System.out.println("```````````````"+request);
        assert request != null;
        User user1 = new User();
        user1.setUserId(userId);
        user1.setLoginIp(request.getRemoteAddr());
        user1.setLoginDate(new Date());
        this.userDao.update(user1);
    }
}